/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

/**
 * Post stellt einen Post in einem Beitrag da. Eine Menge an Posts beschreiben
 * den Inhalt eines Articel und ein Post alleine ist wie ein Unterpunkt. Er
 * beinhaltet eine Unterüberschrift und einen dazugehörigen Text. Außerdem kann
 * er ein Bild speichern
 *
 * @author Patrick Kuhlmann
 */
@RequestScoped
@Entity
public class Post implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String subheading;
    @Lob
    private String post;
    private int post_versionNumber;
    @ManyToOne(cascade = CascadeType.ALL)
    private User creator;
    private String picturePath;
    @ManyToMany(mappedBy = "posts", cascade = CascadeType.ALL)
    List<Version> version;

    public Post(String subheading, String post, String picturePath) {
        this.subheading = subheading;
        this.post = post;
        this.picturePath = picturePath;
    }
     public Post(String subheading, String post, String picturePath, User creator) {
        this.subheading = subheading;
        this.post = post;
        this.picturePath = picturePath;
        this.creator=creator;
    }

    public Post() {
    }

    public String getShortPost() {
        if (post.length() <= 100) {
            return post;
        } else {
            return (post.substring(0, 100) + "...");
        }
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSubheading() {
        return subheading;
    }

    public void setSubheading(String subheading) {
        this.subheading = subheading;
    }

    public String getPost() {        
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }

    public String getPicturePath() {
        return picturePath;
    }

    public void setPicturePath(String picturePath) {
        this.picturePath = picturePath;
    }

    public int getPost_versionNumber() {
        return post_versionNumber;
    }

    public void setPost_versionNumber(int post_versionNumber) {
        this.post_versionNumber = post_versionNumber;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public List<Version> getVersion() {
        return version;
    }

    public void setVersion(List<Version> version) {
        this.version = version;
    }
}
